package com.ruoyi.rulengine.evaluator;

import com.ruoyi.iotcommon.domain.DeviceDataMessage;
import com.ruoyi.rulengine.domain.RuleCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DefaultRuleConditionEvaluator implements RuleConditionEvaluator{

    private static final String GREATER_THAN = ">";
    private static final String LESS_THAN = "<";
    private static final String EQUAL = "=";
    private static final String GREATER_THAN_OR_EQUAL = ">=";
    private static final String LESS_THAN_OR_EQUAL = "<=";
    private static final String NOT_EQUAL = "!=";

    /**
     * 表达式计算后续再支持
     * @param condition
     * @param deviceData
     * @return
     */
    @Override
    public boolean evaluate(RuleCondition condition, DeviceDataMessage deviceData) {
        String propertyKey = condition.getPropertyKey();
        String operatorType = condition.getOperatorType();
        String propertyValue = condition.getPropertyValue();

        // 从设备数据的 params 中获取属性值
        Object actualValue = deviceData.getParams().get(propertyKey);

        if (actualValue == null) {
            log.warn("属性不存在: {}", propertyKey);
            return false;
        }

        // 根据比较符进行判断，也可以查询数据库根据属性设置的类型去转换
        if (actualValue instanceof Number) {
            // 数字类型比较
            double actualNum = ((Number) actualValue).doubleValue();
            double conditionNum;
            try {
                conditionNum = Double.parseDouble(propertyValue);
            } catch (NumberFormatException e) {
                log.warn("条件值不是有效数字: {}", propertyValue);
                return false;
            }

            return switch (operatorType) {
                case GREATER_THAN -> actualNum > conditionNum;
                case LESS_THAN -> actualNum < conditionNum;
                case EQUAL -> actualNum == conditionNum;
                case GREATER_THAN_OR_EQUAL -> actualNum >= conditionNum;
                case LESS_THAN_OR_EQUAL -> actualNum <= conditionNum;
                case NOT_EQUAL -> actualNum != conditionNum;
                default -> throw new IllegalArgumentException("未知的比较符: " + operatorType);
            };
        } else if (actualValue instanceof Boolean) {
            // 布尔类型比较
            if (!(operatorType.equals(EQUAL) || operatorType.equals(NOT_EQUAL))) {
                throw new IllegalArgumentException("布尔类型只支持 = 和 != 操作符");
            }
            boolean conditionBool = Boolean.parseBoolean(propertyValue);
            boolean actualBool = (Boolean) actualValue;
            return operatorType.equals(EQUAL) == (actualBool == conditionBool);
        } else {
            // 字符串或其他类型比较
            String actualStr = String.valueOf(actualValue);
            return switch (operatorType) {
                case EQUAL -> actualStr.equals(propertyValue);
                case NOT_EQUAL -> !actualStr.equals(propertyValue);
                default -> throw new IllegalArgumentException("字符串类型只支持 = 和 != 操作符");
            };
        }
    }

}
